//
//  Emotions.swift
//  表情键盘
//
//  Created by mac on 15/5/23.
//  Copyright (c) 2015年 mac. All rights reserved.
//

import UIKit
//--------------step 9---表情模型----------------------------------------------------------

class Emotions: NSObject {
    
    //分组名
    var emoticon_group_name: String?
    // 分组路径
    var emoticon_group_path: String?
    // 表情图片
    var png: String?
    // 表情的文字
    var chs: String?
    // emoji 编码
    var code: String?
    var isEmptyButton = false ///Step: 15 给要添加删除按钮的 设置标记
    
    var imagePath: String?{
        if png != nil{
            return emoticon_group_path?.stringByAppendingPathComponent(png!) ///Step: 16
        }
        return nil
    }
    var emojiStr:String? ///Step: 19 国际通用表情符号
  
    
    init(group_name: String, group_path: String, dict: [String: String]?) ///Step: 这里字典问号的原因是 有的表情没有 phg chs  code属性
    {
        super.init()
        emoticon_group_name = group_name
        emoticon_group_path = group_path
        png = dict?["png"]
        chs = dict?["chs"]
        code = dict?["code"]
        
        //--------------step 19---国际通用表情符号----------------------------------------------------------
        if code != nil
        {
            var scaner = NSScanner(string: code!)
            var result: UInt32 = 32
            scaner.scanHexInt(&result)
            emojiStr = "\(Character(UnicodeScalar(result)))"
        }
    }

    //--------------step 10---返回模型数组----------------------------------------------------------
    class  func emostionsList() -> [Emotions]
    {
        
        var list = [Emotions]()
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")
        let array = (NSArray(contentsOfFile: path!) as! [[String:String]]).sorted { (dict1, dict2) -> Bool in
            return dict1["emoticon_group_type"] < dict2["emoticon_group_type"]
        }
        for dict in array
        {
            
            let group_path = NSBundle.mainBundle().bundlePath.stringByAppendingPathComponent("Emoticons.bundle").stringByAppendingPathComponent(dict["emoticon_group_path"]!)
            println(group_path)
            list += emostionGroup(dict["emoticon_group_name"]!, group_path: group_path)
        }
        
        
        return list
    }
    
    //--------------step 11---返回每一个表情类型的表情数组----------------------------------------------------------
    
    private class func emostionGroup(group_Name: String, group_path:String) -> [Emotions]
    {
        var list = [Emotions]()
        let path = group_path.stringByAppendingPathComponent("info.plist")
        let dict = NSDictionary(contentsOfFile: path)!
        let array = dict["emoticon_group_emoticons"] as! [[String: String]]
        
        //--------------step 12---遍历每一个分组的表情数组 创建表情对象----------------------------------------------------------
        
        var count = 0
        for emotion in array
        {
            let e = Emotions(group_name: group_Name, group_path: group_path, dict: emotion)
            list.append(e)
            ///Step: 13 每20个插入一个删除按钮
            count++
            if count == 20
            {
                let xx = Emotions(group_name: group_Name, group_path: group_path, dict: nil)
                list.append(xx)
                xx.isEmptyButton = true
                count = 0
            }
            
        }

        //--------------step 14---在末尾不足一页的补齐删除按钮----------------------------------------------------------
        var emptyCount = list.count % 21
        if emptyCount != 0
        {
            for i in emptyCount..<21
            {
                let xx = Emotions(group_name: group_Name, group_path: group_path, dict: nil)
                list.append(xx)
                
            }
          list.last?.isEmptyButton=true
        }
        //--------------step 12---打印每个表情对象----------------------------------------------------------
        var idx = 0
        println("----------------------------------------------------")
        for e in list
        {
            println("\(idx++) \(e.emoticon_group_name) \(e.chs) \(e.code) \(e.isEmptyButton)")

        }
                
        return list
        
    }
    
}


